<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;

/**
 * App\Models\Node
 *
 * @property int $id
 * @property int|null $node_id
 * @property string $title
 * @property string|null $icon
 * @property string|null $banner
 * @property string|null $description
 * @property int $order
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Article[] $articles
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Node[] $children
 * @property-read \App\Models\Node $parent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node filter($input = array(), $filter = null)
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node leaf()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Node onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node root()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereBanner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereIcon($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereNodeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Node withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Node withoutTrashed()
 * @mixin \Eloquent
 * @property int|null $status
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereStatus($value)
 */
class Node extends Model
{
    use SoftDeletes, Filterable;

    protected $fillable = [
        'node_id', 'title', 'icon', 'banner', 'description', 'order'
    ];

    protected $casts = [
        'id' => 'int',
        'node_id' => 'int',
        'order' => 'int',
    ];


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(self::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(self::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function articles()
    {
        return $this->hasMany(Article::class);
    }

    /**
     * @param Builder $query
     * @return mixed
     */
    public function scopeRoot($query)
    {
        return $query->whereNull('node_id');
    }

    /**
     * @param Builder $query
     * @return mixed
     */
    public function scopeLeaf($query)
    {
        return $query->whereNotNull('node_id');
    }

}